﻿using HaiFengShop.Entity;
using HaiFengShop.ORM;
using HaiFengShop.ORM.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;

namespace HaiFengShop.Service
{
    public class SettingsService : BaseServer<tb_settings>
    {
        private readonly ResponseMessage res;
        private readonly GenericJwtToken genericJwtToken;
        public SettingsService(ResponseMessage responseMessage,
             GenericJwtToken genericJwtToken,
             SugarDbContext sugarDbContext) : base(sugarDbContext.Db)
        {
            this.res = responseMessage;
            this.genericJwtToken = genericJwtToken;
        }
        /// <summary>
        /// 获取用户个人信息
        /// </summary> 
        public ResponseMessage GetUserDetail()
        {
            var info = Context.Queryable<tb_user>()
                .Where(s => s.id == genericJwtToken.Uid)
                .First();

            res.data = info;
            return res;

        }

        /// <summary>
        /// 保存用户个人信息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="mobile"></param>
        /// <returns></returns>
        public ResponseMessage SaveUserInfo(string name, string mobile)
        {

            if (string.IsNullOrEmpty(name) || string.IsNullOrEmpty(mobile))
            {
                res.errno = 100;
                res.errmsg = "用户名或手机号不能为空哦";
                return res;
            }
            if (mobile.Length < 11)
            {
                res.errno = 200;
                res.errmsg = "手机号长度不对哦";
                return res;
            }
            if (!System.Text.RegularExpressions.Regex.IsMatch(mobile, @"^1[3456789]\d{9}$"))
            {
                res.errno = 100;
                res.errmsg = "手机号错误哦";
                return res;
            }

            var user = Context.Queryable<tb_user>()
                .Where(s => s.id == genericJwtToken.Uid)
                .First();
            user.name = name;
            user.mobile = mobile;
            user.name_mobile = 1;
            Context.Updateable<tb_user>(user).ExecuteCommand();
            res.data = user;
            return res;

        }

        /// <summary>
        /// 设置
        /// </summary> 
        public ResponseMessage ShowSettings()
        {

            tb_settings settings = Context.Queryable<tb_settings>()
                    .Where(s => s.id == 1)
                    .First();
            res.data = new
            {
                banner = 1,
                channel = 1,
                id = 1,
                index_banner_img = 1,
                notice = 1
            };

            return res;

        }

    }
}
